package main

import (
	"fmt"
	"image"
	"os"

	tiff "github.com/chai2010/tiff"
)

func main() {
	// Load the TIFF file
	filename := "./demdata/ClipResult.tif"
	file, err := os.Open(filename)
	if err != nil {
		fmt.Printf("Failed to open TIFF file: %v\n", err)
		return
	}
	defer file.Close()

	// Get image config to understand the data type
	cfg, err := tiff.DecodeConfig(file)
	if err != nil {
		fmt.Printf("Failed to decode config: %v\n", err)
		return
	}
	
	fmt.Printf("Image config: %+v\n", cfg)
	fmt.Printf("Color model: %T\n", cfg.ColorModel)
	
	// Reset file pointer
	file.Seek(0, 0)
	
	// Decode the image
	img, err := tiff.Decode(file)
	if err != nil {
		fmt.Printf("Failed to decode TIFF: %v\n", err)
		return
	}
	
	fmt.Printf("Image bounds: %+v\n", img.Bounds())
	fmt.Printf("Image type: %T\n", img)
	
	// Check a few pixel values around our test coordinates
	// Our test coordinates convert to pixel (9361, 5669)
	x, y := 9361, 5669
	
	// Check the pixel value
	color := img.At(x, y)
	fmt.Printf("Pixel color at (%d, %d): %+v\n", x, y, color)
	
	// Try to get as RGBA
	r, g, b, a := color.RGBA()
	fmt.Printf("RGBA values: R=%d, G=%d, B=%d, A=%d\n", r, g, b, a)
	
	// For 16-bit signed integer data, we need to handle it differently
	// Let's try to access the underlying data if possible
	switch img := img.(type) {
	case *image.Gray16:
		fmt.Printf("Image is Gray16\n")
		value := img.Gray16At(x, y)
		fmt.Printf("Gray16 value: %+v\n", value)
		fmt.Printf("Gray16 Y value: %d\n", value.Y)
	case *image.NRGBA64:
		fmt.Printf("Image is NRGBA64\n")
		value := img.NRGBA64At(x, y)
		fmt.Printf("NRGBA64 value: %+v\n", value)
	default:
		fmt.Printf("Image type: %T\n", img)
	}
}
